package com.example.lirisheng.springbootfinalexperiment.service;

import com.example.lirisheng.springbootfinalexperiment.entity.RecordTemperature;
import com.example.lirisheng.springbootfinalexperiment.entity.UserRole;
import com.example.lirisheng.springbootfinalexperiment.entity.Users;
import com.example.lirisheng.springbootfinalexperiment.entity1.RecordTempEntity;
import com.example.lirisheng.springbootfinalexperiment.entity1.ResAuthoritiesEntity;
import com.example.lirisheng.springbootfinalexperiment.entity1.ResidentEntity;
import com.example.lirisheng.springbootfinalexperiment.repositories.*;
import com.example.lirisheng.springbootfinalexperiment.util.GeneralUtil;
import com.example.lirisheng.springbootfinalexperiment.util.RespBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import sun.java2d.pipe.RegionSpanIterator;

import java.util.*;

@Service
public class CheService {

    @Autowired
    RecordTemperatureRepository recordTemperatureRepository;

    @Autowired
    UsersRepository usersRepository;

    @Autowired
    PersonInformationRepository personInformationRepository;

    @Autowired
    AuthoritiesRepository authoritiesRepository;

    @Autowired
    UserRoleRepository userRoleRepository;

    public final Logger logger = LoggerFactory.getLogger(ResService.class);


    @Transactional(rollbackFor = Exception.class)
    public void addTemp(String resOpenId, RecordTemperature recordTempEntity) throws  Exception{
        //添加体温记录
            Users users= usersRepository.findByOpenId(resOpenId);
            recordTempEntity.setResId(users.getId());
            recordTempEntity= recordTemperatureRepository.save(recordTempEntity);

            //判断温度是否正常
            if(recordTempEntity.getTemperature()>36.7){
                personInformationRepository.updateAddByResId(users.getId());
            }
            else{
                personInformationRepository.updateZeroByResId(users.getId());
            }
            //添加进入的次数
            personInformationRepository.updateEntityTimeByResId(users.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    public RespBean bandOpenId(Users user1) throws  Exception{

            //验证密码是否正确
            Users users= usersRepository.findByUsernameAndPassword(user1.getUsername(),user1.getPassword());
            if(users==null){
                return RespBean.error("用户名不存在或者密码不对");
            }
            //成功,更新
            if(users.getEnabled()==0){
                return RespBean.error("账号被锁");
            }
            usersRepository.UpdateOpenIdByUsername(user1.getOpenId(),user1.getUsername());
            return RespBean.error("绑定成功");

    }

    public List<Users> getChe(){
        List<Users> users=new ArrayList<Users>();
        Set<Integer> userIds = userRoleRepository.findUserIdByRole("ADMIN");
        Iterator<Users> usersIterator = usersRepository.findAllById(userIds).iterator();
        usersIterator.forEachRemaining(users::add);
        return  users;
    }

    @Transactional(rollbackFor = Exception.class)
    public void addChe(Users users) throws  Exception{
             Users users1=usersRepository.save(users);
             List< UserRole> userRoles = new LinkedList<>();
             GeneralUtil.setCHECKRoleForUser(userRoles,users1.getId());
             userRoleRepository.saveAll(userRoles);
    }

    @Transactional(rollbackFor = Exception.class)
    public void lockAccount(Long id){
            usersRepository.UpdateEnableByResId(0,id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void unLockAccount(Long id) throws  Exception{

            usersRepository.UpdateEnableByResId(1,id);
    }

    public Map<String ,Object>  authenticatedByOpenId(String openId){
        // 进行判断openId存在,MINADMIN, enable=1
        Map<String,Object> map = new LinkedHashMap<String,Object>();
        Users users = usersRepository.findByOpenId(openId);
        if(users==null){
            //需要微信号没有绑定账号
            map.put("status",3);
            map.put("message","微信号没有绑定账号");
            map.put("openId",openId);
            return  map;
        }

        if(users.getEnabled()==0){
            //该账号被锁,无法使用
            map.put("status",4);
            map.put("message","该账号被锁,无法使用");
            map.put("openId",users);
            return  map;
        }

//        ResAuthoritiesEntity resAuthoritiesEntity = authoritiesRepository.findByUsername(users.getUsername());
//        if(resAuthoritiesEntity!=null&&"MINADMIN".equals(resAuthoritiesEntity.getAuthority())){
//           //验证成功
//            map.put("status",1);
//            map.put("message","成功");
//            map.put("openId",users);
//            return  map;
//        }
        //权限不对
        map.put("status",5);
        map.put("message","权限不对");
        map.put("openId",users);
        return  map;

    }




}
